package cn.wegoteam.shop.service;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import cn.wegoteam.shop.inter.BaseDaoInter;
import cn.wegoteam.shop.inter.ProductServiceInter;
import cn.wegoteam.shop.po.Product;
import cn.wegoteam.shop.util.MyString;

@Service
public class ProductService extends BaseService<Product> implements
		ProductServiceInter {
	@Resource(name="productDao")
	public void setBaseDao(BaseDaoInter<Product> baseDao) {
		super.setBaseDao(baseDao);
	}
	
	public int updateBySql(Product p){
		if(p.getId()==null)
			return 0;
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuilder sql=new StringBuilder("UPDATE product SET id=id ");
		if(!MyString.isEmpty(p.getBrand())){
			sql.append(",brand=:brand");
			map.put("brand",p.getBrand());
		}
		if(p.getFlag()!=null){
			sql.append(",flag=:flag");
			map.put("flag",p.getFlag());
		}
		if(!MyString.isEmpty(p.getImgUrl())){
			sql.append(",imgUrl=:imgUrl");
			map.put("imgUrl",p.getImgUrl());
		}
		if(!MyString.isEmpty(p.getContent())){
			sql.append(",content=:content");
			map.put("content",p.getContent());
		}
		if(!MyString.isEmpty(p.getMessage())){
			sql.append(",message=:message");
			map.put("message",p.getMessage());
		}
		if(!MyString.isEmpty(p.getName())){
			sql.append(",name=:name");
			map.put("name",p.getName());
		}
		if(p.getPifaPrice()!=null){
			sql.append(",pifaPrice=:pifaPrice");
			map.put("pifaPrice",p.getPifaPrice());
		}
		if(p.getPrice()!=null){
			sql.append(",price=:price");
			map.put("price",p.getPrice());
		}
		if(p.getScores()!=null){
			sql.append(",scores=:scores");
			map.put("scores",p.getScores());
		}
		if(p.getSellNum()!=null){
			sql.append(",sellNum=:sellNum");
			map.put("sellNum",p.getSellNum());
		}
		if(p.getRemainder()!=null){
			sql.append(",remainder=:remainder");
			map.put("remainder",p.getRemainder());
		}
		if(!MyString.isEmpty(p.getType())){
			sql.append(",type=:type");
			map.put("type",p.getType());
		}
		if(!MyString.isEmpty(p.getAttr())){
			sql.append(",attr=:attr");
			map.put("attr",p.getAttr());
		}
		if(p.getCommentNum()!=null){
			sql.append(",commentNum=:commentNum");
			map.put("commentNum",p.getCommentNum());
		}
		if(p.getMarketPrice()!=null){
			sql.append(",marketPrice=:marketPrice");
			map.put("marketPrice",p.getMarketPrice());
		}
		if(p.getFavorite()!=null){
			sql.append(",favorite=:favorite");
			map.put("favorite",p.getFavorite());
		}
		
		sql.append(" WHERE id="+p.getId());	
		return super.executeBySql(sql.toString(),map);
	}
}
